/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmLliquidaciones.java
 *
 * Created on 3/11/2012, 12:28:00 PM
 */
package co.edu.unitecnologica.interfaz;

import co.edu.unitecnologica.modelo.Conceptos;
import co.edu.unitecnologica.modelo.ConceptosLiquidaciones;
import co.edu.unitecnologica.modelo.Estudiantes;
import co.edu.unitecnologica.modelo.EstudiantesPK;
import co.edu.unitecnologica.modelo.Liquidaciones;
import co.edu.unitecnologica.modelo.Programas;
import co.edu.unitecnologica.reportes.AdmReportes;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.persistence.Query;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Geis
 */
public class FrmLiquidaciones extends Formulario {

    /** Creates new form FrmLliquidaciones */

    public FrmLiquidaciones(Principal p) {
        super(p);
        initComponents();
        init();
        habilitarCampos(false);
   }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jmiImprimirComprobante = new javax.swing.JMenuItem();
        jmiActivarLiquidacion = new javax.swing.JMenuItem();
        jmiAdmConceptosLiq = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtcod_estu = new javax.swing.JTextField();
        txtnombre = new javax.swing.JTextField();
        txtapellido = new javax.swing.JTextField();
        txtperiodo_liq = new javax.swing.JTextField();
        txtcomprobante = new javax.swing.JTextField();
        jcbProgramasliq = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        chkestado = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtConceptos = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();

        jmiImprimirComprobante.setText("Imprimir orden de Matricula");
        jmiImprimirComprobante.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiImprimirComprobanteActionPerformed(evt);
            }
        });

        jmiActivarLiquidacion.setText("Activar Liquidacion");
        jmiActivarLiquidacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiActivarLiquidacionActionPerformed(evt);
            }
        });

        jmiAdmConceptosLiq.setText("Administrar Conceptos de Liquidacion");
        jmiAdmConceptosLiq.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiAdmConceptosLiqActionPerformed(evt);
            }
        });

        setTitle("Liquidaciones");

        jLabel1.setText("ID:");

        jLabel2.setText("Nombres:");

        jLabel3.setText("Apellidos:");

        jLabel4.setText("Programa:");

        jLabel5.setText("Periodo Liquidacion:");

        jLabel7.setText("Nro Comprobante:");

        txtcod_estu.setEditable(false);

        txtnombre.setEditable(false);

        txtapellido.setEditable(false);

        txtperiodo_liq.setEditable(false);

        txtcomprobante.setEditable(false);

        jLabel8.setText("Activado:");

        jtConceptos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Descripcion", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Float.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jtConceptos);

        jLabel6.setText("Conceptos de liquidación");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel5)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel8))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chkestado)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtnombre, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
                                .addComponent(txtapellido, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
                                .addComponent(jcbProgramasliq, 0, 331, Short.MAX_VALUE)
                                .addComponent(txtcod_estu, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtperiodo_liq, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtcomprobante, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jLabel6)
                        .addGap(293, 293, 293)))
                .addContainerGap(45, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtcod_estu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtnombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtapellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbProgramasliq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtperiodo_liq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtcomprobante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(chkestado))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(50, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jmiImprimirComprobanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiImprimirComprobanteActionPerformed
        Liquidaciones l=(Liquidaciones) lista.get(getRegistroActual());
        em.getTransaction().begin();
        AdmReportes adm= new AdmReportes(em.unwrap(java.sql.Connection.class));
        adm.imprimirComprobanteEstudiante(l.getLiquidacionesPK().getIdEstudiante(), String.valueOf(l.getLiquidacionesPK().getCodPeriodo()));
        em.getTransaction().commit();
    }//GEN-LAST:event_jmiImprimirComprobanteActionPerformed

    private void jmiActivarLiquidacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiActivarLiquidacionActionPerformed
        if(principal.validarObjeto("FrmActivaciones")){
            Liquidaciones l=(Liquidaciones) lista.get(getRegistroActual());
            FrmActivaciones a= new FrmActivaciones(principal,l);
            principal.getEscritorio().add(a);
            a.setVisible(true);
        }
        else
            JOptionPane.showMessageDialog(rootPane, "Lo siento, pero su cuenta no tiene permisos para acceder a este Objeto");
    }//GEN-LAST:event_jmiActivarLiquidacionActionPerformed

    private void jmiAdmConceptosLiqActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiAdmConceptosLiqActionPerformed
        if(principal.validarObjeto("FrmConceptosLiquidaciones")){
            Liquidaciones l=(Liquidaciones) lista.get(getRegistroActual());
            FrmConceptosLiquidaciones a= new FrmConceptosLiquidaciones(principal,l);
            principal.getEscritorio().add(a);
            a.setVisible(true);
        }
        else
            JOptionPane.showMessageDialog(rootPane, "Lo siento, pero su cuenta no tiene permisos para acceder a este Objeto");
    }//GEN-LAST:event_jmiAdmConceptosLiqActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox chkestado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox jcbProgramasliq;
    private javax.swing.JMenuItem jmiActivarLiquidacion;
    private javax.swing.JMenuItem jmiAdmConceptosLiq;
    private javax.swing.JMenuItem jmiImprimirComprobante;
    private javax.swing.JTable jtConceptos;
    private javax.swing.JTextField txtapellido;
    private javax.swing.JTextField txtcod_estu;
    private javax.swing.JTextField txtcomprobante;
    private javax.swing.JTextField txtnombre;
    private javax.swing.JTextField txtperiodo_liq;
    // End of variables declaration//GEN-END:variables

    @Override
    public boolean insertarRegistro() {
        JOptionPane.showMessageDialog(this, "Operación no permitida para este formulario");
        return false;
    }

    @Override
    public boolean buscarRegistros() {
        
         try{
            String sql="SELECT l.* "
                    + " FROM liquidaciones l "
                    + " WHERE "
                    + "     l.id_estudiante like '%"+txtcod_estu.getText()+"%' "
                    + "     and l.numero_comprobante like '%"+this.txtcomprobante.getText()+"%' "
                    + "     and l.cod_periodo like '%"+txtperiodo_liq.getText()+"%' "
                    + "     and l.cod_programa like'"+this.jcbProgramasliq.getSelectedItem().toString().split("-")[0] +"%' ";
                  
            query = em.createNativeQuery(sql,Liquidaciones.class);
            query.setHint("javax.persistence.cache.storeMode", "REFRESH");
            lista= query.getResultList();
            this.setRegistroActual(0);
            this.setRegistrosTotales(lista.size());
            this.mostrarRegistro(0);
            return true;
        }catch(Exception e){
            return false;
        }
       
    }

    @Override
    public boolean editarRegistro() {
        JOptionPane.showMessageDialog(this, "Operación no permita para esta forma");
        return false;
    }

    @Override
    public boolean borrarRegistro() {
        try{
            em.getTransaction().begin();
            Liquidaciones l=(Liquidaciones) lista.get(getRegistroActual());
            em.remove(l);
            em.getTransaction().commit();
            return true;
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, e.getLocalizedMessage());
            return false;
        }

    }

    @Override
    public void cargarRegistros() {
        query=em.createNamedQuery("Liquidaciones.findAll");
        query.setHint("javax.persistence.cache.storeMode", "REFRESH");
        lista=query.getResultList();
        setRegistroActual(0);
        setRegistrosTotales(lista.size());
        mostrarRegistro(getRegistroActual());
    }

    @Override
    public void limpiarCampos() {
        txtcod_estu.setText("");
        txtnombre.setText("");
        txtapellido.setText("");
        txtperiodo_liq.setText("");
        txtcomprobante.setText("");
        jcbProgramasliq.setSelectedIndex(0);
        DefaultTableModel modelo=(DefaultTableModel) jtConceptos.getModel();
        modelo.setRowCount(0);
        jtConceptos.setModel(modelo);
    }

    @Override
    public void habilitarCampos(boolean v) {
       this.txtcod_estu.setEditable(v);
       this.txtcomprobante.setEditable(v);
       this.txtperiodo_liq.setEditable(v);
       this.jtConceptos.setEnabled(v);
       this.jcbProgramasliq.setEditable(v);
    }

    @Override
    public void agregarItemsMenuOpciones() {
        principal.getJmOpciones().add(jmiAdmConceptosLiq);
        principal.getJmOpciones().add(jmiImprimirComprobante);
        principal.getJmOpciones().add(jmiActivarLiquidacion);
    }

    @Override
    public void mostrarRegistro(int r) {
        
        Liquidaciones l = (Liquidaciones) lista.get(r);
        EstudiantesPK pk=new EstudiantesPK();
        pk.setId(l.getLiquidacionesPK().getIdEstudiante());
        pk.setCodPrograma(l.getLiquidacionesPK().getCodPrograma());
        Estudiantes e=em.getReference(Estudiantes.class,pk);
        this.txtcod_estu.setText(l.getLiquidacionesPK().getIdEstudiante());
        this.txtnombre.setText(e.getNombres());
        this.txtapellido.setText(e.getApellidos());
        Programas p=em.getReference(Programas.class, l.getLiquidacionesPK().getCodPrograma());
        this.jcbProgramasliq.setSelectedItem(p.getCod()+"-"+p.getDescripcion());
        this.txtperiodo_liq.setText(l.getPeriodos().getCod().toString());
        this.txtcomprobante.setText(String.valueOf(l.getNumeroComprobante()));
        if(l.getEstado().equals("liquidado")){
            this.chkestado.setSelected(false);
        }
        else{
            this.chkestado.setSelected(true);
        }
        this.llenarTabla(l);
    }
    
    private void init() {
        Query q = em.createNamedQuery("Programas.findAll");
        List l = q.getResultList();
        jcbProgramasliq.addItem("");
        for(int i=0;i<l.size();i++){
            Programas t= (Programas)l.get(i);
            jcbProgramasliq.addItem(t.getCod()+"-"+t.getDescripcion());
        }
    
    }
    public void llenarTabla(Liquidaciones li){
        DefaultTableModel modelo=(DefaultTableModel) jtConceptos.getModel();
        modelo.setRowCount(0);
        Collection<ConceptosLiquidaciones> conceptos_liq = li.getConceptosLiquidacionesCollection();
        Iterator<ConceptosLiquidaciones> it_conceptos = conceptos_liq.iterator();
        while(it_conceptos.hasNext()){
            ConceptosLiquidaciones conceptol = it_conceptos.next();
            Double valor=conceptol.getValor();
            Conceptos concepto=em.getReference(Conceptos.class, conceptol.getConceptosLiquidacionesPK().getCodConcepto());
            String descripcion=concepto.getDescripcion();
            Object[] registro= new Object[2];
            registro[0]=descripcion;
            registro[1]=valor;
            modelo.addRow(registro);
        }
        this.jtConceptos.setModel(modelo);        
    }
}
